 <?php 
 	//运费模版记录分类
 	if(isset($model->logs)){
 		foreach($model->logs as $val){
 			if($val->zel_area==''){
 				$freights[$val->zel_type]['all'] = $val;
 			}else{
 				$freights[$val->zel_type]['area'][] = $val;
 			}
 		}
 	}
 	//print_r($freights);exit;
 ?>
 
 <div class="hr_15"></div>
 <div class="address_manage">
     <div class="add_address add_fre_temp">
		 <div class="hd"><h3>新增运费模板</h3><h3 class="d_n">修改运费模板</h3></div>
		 <div class="bd">
			<?php $form=$this->beginWidget('CActiveForm', array(
					'id'=>'address-form',
					'action'=>!empty($model->ze_id)?$this->pre_module_url.'/express/freightupdate':$this->pre_module_url.'/express/freightcreate',
					'enableAjaxValidation'=>false,
				)); ?>
				<?php echo $form->HiddenField($model,'ze_id')?>
				<?php echo $form->HiddenField($model,'zm_id')?>
				<ul class="form_list">
					<li>
						<span class="tit">模板名称：</span>
						<?php echo $form->textField($model,'ze_title',array('class'=>'t_ipt ipt_bg')); ?>
						<span id="Express_ze_titleTip"></span>
					 <li><span class="tit">宝贝地址：</span>
						<?php Area::getAreainfo('Express',$model->zar_id,'clasname'); ?>
						<span id="MemAddres_zar_idTip"></span>
					 </li>
					 <li><span class="tit">详细地址：</span>
						<?php echo $form->textField($model,'ze_addr',array('class'=>'t_ipt ipt_bg')); ?>
						<span id="Express_ze_addrTip"></span>
					 </li>
					 <li class="pricing"><span class="tit">计价方式：</span>
					 	<?php $priceing = Express::model()->getPricing() ;
					 		foreach($priceing as $key=>$val){
					 			echo '<span class="mr"><input value="'.$key.'" onclick="selectunit(\''.$key.'\')" type="radio"';
					 			if($key==$model->ze_valuation)
					 				echo 'checked="checked"';
 								echo 'name="Express[ze_valuation]"><label for="">'.$val.'</label></span>';
					 		}
					 	?>
					 	<?php //echo $form->radioButtonList($model,'ze_valuation',Express::model()->getPricing())?>
					 </li>
					 <li class="c_f"><span class="tit">运送方式：</span>
						<p class="c_999">除指定地区处，其它地区的运费采用"默认运费"</p>
					<li class="c_f">	
						<span class="tit">&nbsp;</span>
						<div class="cont f_l">
						 <ul class="type">
						 	<?php $freighttype = Express::model()->getFreightType();foreach($freighttype as $key=>$val){?>
							<li>
								<p><input class="chk freighttype" type="checkbox" name="freighttype" <?php if(isset($freights[$key])) echo 'checked';?> /><?php echo $val ?></p>
								 <div class="freight_set <?php if(!isset($freights[$key])) echo 'hide';?>">
									<div class="set">默认运费：
										<?php echo CHtml::hiddenField('freight[zel_type][]',$key)?>
										<input type="hidden" value="" name="freight[zel_area][]" />
										<input class="t_ipt" type="text" value="<?php if(isset($freights[$key])) echo $freights[$key]['all']['zel_start_num'];?>" name="freight[zel_start_num][]" maxlength="10" /><span class="uni">m3</span>内，
										<input class="t_ipt" type="text" value="<?php if(isset($freights[$key])) echo $freights[$key]['all']['zel_start_price'];?>" name="freight[zel_start_price][]" maxlength="10" />元，每增加
										<input class="t_ipt" type="text" value="<?php if(isset($freights[$key])) echo $freights[$key]['all']['zel_add_num'];?>" name="freight[zel_add_num][]" maxlength="10" /><span class="uni">m3</span>，增加运费 
										<input class="t_ipt" type="text" value="<?php if(isset($freights[$key])) echo $freights[$key]['all']['zel_add_price'];?>" name="freight[zel_add_price][]" maxlength="10" />元
									</div>
									<div class="other_box mt10 <?php if(!isset($freights[$key])&&!isset($freights[$key]['area'])) echo 'hide';?>">
										<table width="100%" cellpadding="0" cellspacing="0">
											<tr>
												<th>运送到</th>
												 <th width="13%">首<span class="units">体积（m3）</span></th>
												 <th width="13%">首费（元）</th>
												 <th width="13%">续<span class="units">体积（m3）</span></th>
												 <th width="13%">续费（元）</th>
												 <th width="11%">操作</th>
											 </tr>
											 <?php  if(isset($freights[$key])&&isset($freights[$key]['area'])){
											 		foreach($freights[$key]['area'] as $val){
											 	?>
												 <tr>
													 <td class="tl">
														<a class="c_06c f_r setfreight" href="javascript:" title="">编辑</a>
														<p class="freight_area"><?php echo Area::model()->getAreas($val->zel_area)?></p>
														<input type="hidden" value="<?php echo $val->zel_area?>" name="freight[zel_area][]" /><input type="hidden" name="freight[zel_type][]" value="<?php echo $val->zel_type?>">
													 </td>
													 <td><input class="t_ipt" type="text" value="<?php echo $val->zel_start_num?>" name="freight[zel_start_num][]" maxlength="10" /></td>
													 <td><input class="t_ipt" type="text" value="<?php echo $val->zel_start_price?>" name="freight[zel_start_price][]" maxlength="10" /></td>
													 <td><input class="t_ipt" type="text" value="<?php echo $val->zel_add_num?>" name="freight[zel_add_num][]" maxlength="10" /></td>
													 <td><input class="t_ipt" type="text" value="<?php echo $val->zel_add_price?>" name="freight[zel_add_price][]" maxlength="10" /></td>
													 <td><a class="c_06c dele" href="javascript:" title="">删除</a></td>
												 </tr>
											 <?php }}?>
										 </table>
									 </div>
									 <p class="other_set "><a class="c_06c click" href="javascript:" title="">为指定城市设置运费</a></p>
								 </div>
							 </li>
							 <?php }?>
						 </ul>
						 </div>
					 </li> 
					 <li class="submit"><span class="tit">&nbsp;</span>
						 <div class="cont f_l">
						 	<?php echo CHtml::submitButton(Yii::t('cmp','Save'),array('class'=>'mem_btn mem_btn18')); ?>
							<?php echo CHtml::resetButton(Yii::t('cmp','Reset'),array('class'=>'mem_btn mem_btn07')); ?>
						</div>
					 </li>
				 </ul>
			 <?php $this->endWidget(); ?>
		 </div>
	</div>
</div>
<script type="text/javascript">
//选中运送方式
$('.freighttype').bind('click',function(){
	$(this).parent().next('div').toggle();
})

//添加指定城市运费
$('.other_set .click').bind('click',function(){
	var freightType = $(this).parent().prev('div').prev('div').find('input').eq(0).val();
	$(this).parent().prev('div').show();				
	$(this).parent().prev('div').find('table').append('<tr>'
									+' <td class="tl">'
										+'<a class="c_06c f_r setfreight" href="javascript:" title="">编辑</a>'
										+'<p class="freight_area"><span>未添加地区</span></p>'
										+'<input type="hidden" value="" name="freight[zel_area][]" /><input type="hidden" name="freight[zel_type][]" value="'+freightType+'">'
									+' </td>'
									+' <td><input class="t_ipt" type="text" value="" name="freight[zel_start_num][]" maxlength="10" /></td>'
									+' <td><input class="t_ipt" type="text" value="" name="freight[zel_start_price][]" maxlength="10" /></td>'
									+' <td><input class="t_ipt" type="text" value="" name="freight[zel_add_num][]" maxlength="10" /></td>'
									+' <td><input class="t_ipt" type="text" value="" name="freight[zel_add_price][]" maxlength="10" /></td>'
									+' <td><a class="c_06c dele" href="javascript:" title="">删除</a></td>'
									+' </tr>');
})

//删除指定城市运费
$('.dele').live('click',function(){
	if(!$(this).parent().parent().parent().find('tr')[2]){
		$(this).parent().parent().parent().parent().parent().hide();
	}
	$(this).parent().parent().remove();
})



//显示隐藏三级地区
$('.ico_updown').live('click',function(){
	$('.box').removeClass('open');
//	alert('ico');
	$(this).parent().parent().toggleClass("open");
})

//显示隐藏三级地区
$('.open .ico_updown').live('click',function(){
	$('.box').removeClass('open');
})

//隐藏三级地区
/*$('.prov_list').live('click',function(){
	alert("prov");
	$('.box').removeClass('open');
})*/

//一级，二级地区全选
$('.box .chk').live('click',function(){
	$(this).parent().next('div').find("input").attr('checked',true);
})

//地区弹框
$.getJSON('/cache/getarea',function(json){
	$('.setfreight').live('click',function(){
		var str = '<ul class="city_list">';
		str += 		'<li class="c_f">'
			+'			<div class="prov_list">'
			for(var val1 in json){
			str +=			'<div class="box">'
				+'					<div class="ecity">'
				+'						<input class="chk" type="checkbox" name="" value="'+json[val1].zar_id+'"><label>'+json[val1].zar_title+'</label>'
				+'						<span class="check_num">'
			if(json[val1].child){
				str += 					'('+json[val1].child.length+')'
			}
			str +=						'</span><a class="ico ico_updown click" href="javascript:"></a>'
				+'					</div>'
				+'					<div class="citys">'
				+'						<div>';
			for(var val2 in json[val1].child){
					str +=				'<span class="areas"><input class="chk area" type="checkbox" name="area" value="'+json[val1].child[val2].zar_id+'"><label>'+json[val1].child[val2].zar_title+'</label></span>';
			}
			str +=						'</div>'
				+'					</div>'
				+'				</div>'
		}
		str +=			'</div>'
			+'		</li>'
		art.dialog({
			title:'选择地区',
			width:'850px',
			padding:'20px 5px 20px 15px',
			content:str,
			okValue:'确定',
		    ok: function () {
		    	var areatext='' , areavalue='';
		        for(var i=0;i<$('.area:checked').length;i++){
			        	if(areatext==''){
							areatext =  $('.area:checked').eq(i).next('label').text();
							areavalue = $('.area:checked').eq(i).val();
			        	}else{
							areatext +=  ','+$('.area:checked').eq(i).next('label').text();
							areavalue += ','+$('.area:checked').eq(i).val();
				        }
			    }
			    $('#actionarea').replaceWith(areatext);
			    $('#actioninput').replaceWith('<input type="hidden" value="'+areavalue+'" name="freight[zel_area][]" />');
		        return true;
		    }
		});
		$(this).next('p').html("<div id='actionarea'></div>");
		$(this).next('p').next('input').replaceWith('<input type="hidden" id="actioninput" value="" name="freight[zel_area][]" />');
	});
});


//选中计价方式,切换单位
function selectunit(type){
	var uni = new Array(),units = new Array();
	uni[1] = '件';
	uni[2] = 'kg';
	uni[3] = 'm3' ; 
	units[1] = '件(件)';
	units[2] = '重量(kg)';
	units[3] = '体积(m3)';
	$('.uni').text(uni[type]);
	$('.units').text(units[type]);
}

</script>

<script type="text/javascript" src="<?php echo Yii::app()->baseUrl;?>/js/formvalidata/formValidator-4.1.1.js"></script>
<script type="text/javascript" src="<?php echo Yii::app()->baseUrl;?>/js/formvalidata/formValidatorRegex.js"></script>
 <script>
	jQuery(document).ready(function($) {
		$.formValidator.initConfig({theme:"Default",formID:"address-form",
			onError:function(msg, obj){
						alert(msg);
						return false;
					},
			onsuccess:function(){ return true; }
		});
	})
</script>
<script type="text/javascript" src="<?php echo Yii::app()->baseUrl;?>/js/formvalidata/cmpValidate.js"></script>